/*
 Copyright (C) 2010 Chris Hawkins. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
 * Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.
 
 * Neither the name of the author nor the names of its contributors may be used
 to endorse or promote products derived from this software without specific
 prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "DataDomain.h"
#define DASD_MAX_VALUE_ATTRIBUTE_CODE 8
typedef struct {
    unsigned short      code;
    NSString*           name;
    unsigned short      itemCount;
    NSString*           description;
} stDASDDomainItem;

typedef struct {
    unsigned short      itemCode;
    unsigned short      nameAttributeCode;
    unsigned short      valueAttributeCode[DASD_MAX_VALUE_ATTRIBUTE_CODE];
} stDASDDomainItemAttributes;

stDASDDomainItem astDASDDomains[] = {
    { eDASDDomain_Empty        ,@"Empty"                  ,  0,  @"0 Item" },
    { eDASDDomain_OneItem      ,@"OneItem"                ,  1,  @"1 Item (1 Attribute)" },
    { eDASDDomain_TwoItem      ,@"Two_Items"              ,  2,  @"2 Item (4 Attribute)" },
    { eDASDDomain_Small        ,@"Small"                  , 15, @"15 Item (3 Attribute)" },
    { eDASDDomain_Medium       ,@"Medium"                 ,100,@"100 Item (6 Attribute)" },
};
stDASDDomainItemAttributes astDASDDomainAttributes[] = {
    { eDASDDomain_Empty        ,                           0,  { 0,0,0,0,0,0,0,0 }},
    { eDASDDomain_OneItem      ,eDASDAttribute_UniqueMixedKey, { eDASDAttribute_Color ,0,0,0,0,0,0,0 }},
    { eDASDDomain_TwoItem      ,eDASDAttribute_UniqueInteger,  { eDASDAttribute_Color, 
                                                                 eDASDAttribute_Timestamp_Last, 
                                                                 eDASDAttribute_Timestamp_Hist, 
                                                                 eDASDAttribute_LongText, 0,0,0,0 }},
    { eDASDDomain_Small        ,eDASDAttribute_UniqueMixedKey, { eDASDAttribute_Color, 
                                                                 eDASDAttribute_Timestamp_Last, 
                                                                 eDASDAttribute_Fruit, 0,0,0,0,0 }},
    { eDASDDomain_Medium       ,eDASDAttribute_UniqueRowNumber,{ eDASDAttribute_Color, 
                                                                 eDASDAttribute_Fruit,
                                                                 eDASDAttribute_UniqueInteger,
                                                                 eDASDAttribute_Timestamp_Last, 
                                                                 eDASDAttribute_Timestamp_Hist, 
                                                                 eDASDAttribute_LongText,0,0 }},
};
@implementation DASDDomain
@synthesize itemCount = item_Count, itemAttribute = item_Attribute, valueAttributes = value_Attributes;

- (void)                dealloc {
    [item_Attribute         release];
    [value_Attributes       release];
    [item_NameDictionary    release];
    [super dealloc];
}
#pragma mark -- private methods
- (void)                addAttributes: (stDASDDomainItemAttributes*) attributes_ {
    DASDDictionary *dictAttribute   = [[DASDAttribute dictionary] retain];
    NSMutableArray *arrayAttribute  = [[NSMutableArray alloc] initWithCapacity: DASD_MAX_VALUE_ATTRIBUTE_CODE];
    DASDAttribute  *attribute       = (DASDAttribute*) [dictAttribute objectForCodeInt: attributes_->nameAttributeCode];
    if ( attribute != nil ) {
        [self setItemAttribute: attribute];
    }
    for ( int index = 0; index < DASD_MAX_VALUE_ATTRIBUTE_CODE; index++ ) {
        unsigned short code = attributes_->valueAttributeCode[ index ];
        if ( 0 < code ) {
            attribute = (DASDAttribute*) [dictAttribute objectForCodeInt: code];
            if ( attribute != nil ) {
                [arrayAttribute addObject: attribute];
            }
        }
    }
    [self setValueAttributes: [[NSArray alloc] initWithArray: arrayAttribute]];
    [arrayAttribute release];
    [dictAttribute  release];
}
#pragma mark -- public methods
- (NSUInteger)          attributeCount {
    return ( value_Attributes == nil ? 0 : [value_Attributes count] );
}
- (NSString*)           itemAttributeValue: (DASDAttribute*) attribute_ row: (NSUInteger) rowNumber_ {
    return [attribute_ valueForRow: rowNumber_];
}
- (NSDictionary*)       itemNameDictionary {
    if ( item_NameDictionary == nil ) {
        @synchronized( self ) {
            if ( item_NameDictionary == nil ) {
                NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity: (item_Count + 1)];
                for ( int index = 0; index < item_Count; index++ ) {
                    NSString *key   = [NSString stringWithFormat: @"%u", index];
                    NSString *value = [self itemNameForRow: index];
                    [dict setObject: value forKey: key];
                }
                item_NameDictionary = [[NSDictionary alloc] initWithDictionary: dict];
                [dict release];
            }
        }
    }
    return item_NameDictionary;
}
- (NSString*)           itemNameForRow: (NSUInteger) rowNumber_ {
    return ( item_Attribute == nil ? nil : [self itemAttributeValue: item_Attribute row: rowNumber_] );
}
- (NSString*)           valueForRow: (NSUInteger) rowNumber_ valueIndex: (NSUInteger) index_ {
    NSString    *value  = nil;
    if ( [self attributeCount] <= index_ ) {
        value = [NSString stringWithFormat: @"Invalid index: %u for domain: %@",index_, [self entryName]];
    } else {
        DASDAttribute *attribute = [value_Attributes objectAtIndex: index_];
        value = [self itemAttributeValue: attribute row: rowNumber_];
    }

    return value;
}
+ (DASDDictionary*)     dictionary {
    static DASDDictionary   *dictDomain = nil;
    if ( dictDomain == nil ) {
        @synchronized( self ) {
            if ( dictDomain == nil ) {
                DASDDomain  *domain = nil;
                dictDomain = [[DASDDictionary alloc] initWithCapacity: mASDarrayCount( astDASDDomains )];
                stDASDDomainItem *curD = &astDASDDomains[0];
                for ( int countD = 0; countD < mASDarrayCount( astDASDDomains ); countD++, curD++ ) {
                    domain = [[DASDDomain alloc] initWithCodeInt: curD->code name: curD->name desc: curD->description ];
                    [domain setItemCount: curD->itemCount];
                    [dictDomain addEntry: domain];
                    [domain release];
                }
                stDASDDomainItemAttributes *curA = &astDASDDomainAttributes[0];
                for (int countA = 0; countA < mASDarrayCount( astDASDDomainAttributes ); countA++, curA++ ) {
                    domain = [[dictDomain objectForCodeInt: curA->itemCode] retain];
                    if ( domain != nil ) {
                        [domain addAttributes: curA];
                    }
                    [domain release];
                }
            }
        }
    }
    return dictDomain;
}
@end
